require 'rexml/document'
module FamilyTreeApi
  module Person
    module Assertion

      class Place
        attr_accessor :normalized, :original, :place_id
        
        def ==(other)
          !other.nil? && @normalized == other.normalized && @original == other.original || !@place_id.nil? && @place_id == other.place_id
        end
        
        def self.parse(place_element)
          unless place_element.nil?
            place = Assertion::Place.new
            place_element.each do |e|
              case e.name
              when 'normalized'
                place.place_id = e.attributes['placeId']
                place.normalized = e.content
              when 'original'
                place.original = e.content
              end
            end
            place
          end
        end
        
        def to_xml
          e = REXML::Element.new('place')
          e.add_element('original').add_text(@original) if @original
          if @normalized
            normalized = e.add_element('normalized')
            normalized.add_text(@normalized)
            normalized.add_attribute('placeId', @place_id)
          end
          e
        end
        
      end
    end
  end
end